// ignore_for_file: non_constant_identifier_names

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:pp_uikit/theme/pp_default_theme.dart';

class PPUIKitLocalizations {
  final Locale locale;

  PPUIKitLocalizations(this.locale);

  static final Map<String, LocalizationsString> values = {
    'en': EnLocalizationsString(),
    'zh': ChLocalizationsString(),
    // 'fr': FrRefreshString(),
    // 'ru': RuRefreshString(),
    // 'uk': UkRefreshString(),
    // 'it': ItRefreshString(),
    // 'ja': JpRefreshString(),
    // 'de': DeRefreshString(),
    // 'es': EsRefreshString(),
    // 'nl': NlRefreshString(),
    // 'sv': SvRefreshString(),
    // 'pt': PtRefreshString(),
    // 'ko': KrRefreshString(),
  };

  LocalizationsString? get currentLocalization {
    if (values.containsKey(locale.languageCode)) {
      return values[locale.languageCode];
    }
    return values["en"];
  }

  static const PPUIKitLocalizationsDelegate delegate =
      PPUIKitLocalizationsDelegate();

  static PPUIKitLocalizations? of(BuildContext context) {
    return Localizations.of(context, PPUIKitLocalizations);
  }

  static LocalizationsString getLocalizations(BuildContext? context) {
    var supportLanguageCode = PPDefaultTheme.instance.supportLanguageCode;
    if (supportLanguageCode != null) {
      var item = PPUIKitLocalizations.values[supportLanguageCode];
      if (item != null) {
        return item;
      }
    }
    if (context == null) {
      return EnLocalizationsString();
    }
    return PPUIKitLocalizations.of(context)?.currentLocalization ??
        EnLocalizationsString();
  }
}

class PPUIKitLocalizationsDelegate
    extends LocalizationsDelegate<PPUIKitLocalizations> {
  const PPUIKitLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return [
      'en',
      'zh',
      // 'fr',
      // 'ru',
      // 'uk',
      // 'ja',
      // 'it',
      // 'de',
      // 'ko',
      // 'pt',
      // 'sv',
      // 'nl',
      // 'es'
    ].contains(locale.languageCode);
  }

  @override
  Future<PPUIKitLocalizations> load(Locale locale) {
    return SynchronousFuture<PPUIKitLocalizations>(
        PPUIKitLocalizations(locale));
  }

  @override
  bool shouldReload(LocalizationsDelegate<PPUIKitLocalizations> old) {
    return false;
  }
}

/// interface implements different language
abstract class LocalizationsString {
  String retry = "";
  String loading = "";
  String hintText = "";
  String search = "";

  // -------------- PPRefresh --------------
  String idleRefreshText = "";

  String canRefreshText = "";

  String refreshingText = "";

  String refreshCompleteText = "";

  String idleLoadingText = "";

  String canLoadingText = "";

  String loadFailedText = "";

  String noMoreText = "";

  // -------------- PPStateLayout --------------

  /// 空状态提示标题
  String emptyTitleText = "";

  /// 异常状态提示标题
  String exceptionTitleText = "";

  /// 默认自定义异常标题文案
  String stateLayout_customTitleText = "";
}

class ChLocalizationsString implements LocalizationsString {
  @override
  String retry = "重试";

  @override
  String loading = "加载中…";

  @override
  String hintText = "请输入内容";

  @override
  String search = "搜索";

  @override
  String idleRefreshText = "下拉刷新";

  @override
  String canRefreshText = "松开开始刷新数据";

  @override
  String refreshingText = "刷新中…";

  @override
  String refreshCompleteText = "刷新成功";

  @override
  String loadFailedText = "加载失败";

  @override
  String noMoreText = "没有更多数据了";

  @override
  String canLoadingText = "松手开始加载数据";

  @override
  String idleLoadingText = "上拉加载";

  @override
  String emptyTitleText = "暂无相关数据内容";

  @override
  String exceptionTitleText = "网络错误~请稍后重试";

  @override
  String stateLayout_customTitleText = "出错啦~请稍后重试";
}

class EnLocalizationsString implements LocalizationsString {
  @override
  String retry = "Retry";

  @override
  String loading = "Loading…";

  @override
  String hintText = "Please enter text";

  @override
  String search = "Search";

  @override
  String idleRefreshText = "Pull down Refresh";

  @override
  String canRefreshText = "Release to refresh";

  @override
  String refreshCompleteText = "Refresh completed";

  @override
  String refreshingText = "Refreshing…";

  @override
  String loadFailedText = "Load Failed";

  @override
  String noMoreText = "No more data";

  @override
  String canLoadingText = "Release to load more";

  @override
  String idleLoadingText = "Pull up Load more";

  @override
  String emptyTitleText = "Empty data";

  @override
  String exceptionTitleText = "Network error, Please try again later";

  @override
  String stateLayout_customTitleText = "Oops, please try again later";
}
